package com.web.controller.company;

import com.alibaba.dubbo.config.annotation.Reference;
import com.domain.company.Company;
import com.github.pagehelper.PageInfo;
import com.service.company.CompanyService;
import com.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {
    
    @Reference
    private CompanyService companyService;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size){

        PageInfo pageInfo = companyService.findAll(page, size);
        request.setAttribute("page", pageInfo);
        return "company/company-list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "company/company-add";
    }

    @RequestMapping("/edit")
    public String edit(Company company){

        if (StringUtils.isBlank(company.getId())){
            //2、调用companyService进行保存
            companyService.save(company);
        }else {
            companyService.update(company);
        }
        return "redirect:/company/list.do";
    }

    @RequestMapping("/delete")
    public String add(String id){
        companyService.delete(id);
        return "redirect:/company/list.do";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        Company company = companyService.findById(id);
        request.setAttribute("company", company);
        return "company/company-update";
    }



}
